package com.hrbxlh.actionImpl;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.hrbxlh.pojo.SysUser;
import com.hrbxlh.util.SessionValues;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.hrbxlh.dao.AnswerDao;
import com.hrbxlh.dao.CourseDao;
import com.hrbxlh.dao.MyCourseDao;
import com.hrbxlh.dao.ProblemDao;
import com.hrbxlh.util.pageUtil;

import static com.hrbxlh.support.web.auth.constant.ServiceConstant.USER_INFO;


@Component("problemActionImpl")
@Transactional
public class ProblemActionImpl {
	
	@Resource(name="problemDao")
	private ProblemDao problemDao;
	
	@Resource(name="courseDao")
	private CourseDao courseDao;
	
	@Resource(name="answerDao")
	private AnswerDao answerDao;
	
	@Resource(name="myCourseDao")
	private MyCourseDao myCourseDao;
	
	/**
	 * @param pageNow,pageSize(一般是10)，页数从1开始，第一页pageNow传1
	 * @return  问题列表页面（教师）。进页面，翻页，条件查询都是这个。
	 * @throws IOException
	 */
	public Map<String, Object> getAllProblemList(Map<String, Object> paramMap,HttpSession session) throws Exception {

		Map<String, Object> returnMap = new HashMap<String, Object>();
		try {
			
			//获取分页数据
			int pageNow = (Integer)paramMap.get("pageNow");
			int pageSize = (Integer)paramMap.get("pageSize");
			int startNum = pageUtil.getSqlStartPos(pageNow, pageSize);
			paramMap.put("startNum", startNum);
			//获取当前登陆人的学号/工号
			String stuorteaId = (String) session.getAttribute("stuorteaId");
//			String stuorteaId = "8888888";
			returnMap.put("stuorteaId", stuorteaId);
			paramMap.put("stuorteaId", stuorteaId);
			//通过教师号获取此教师所属课程
			List<Map<String,Object>> courseList = courseDao.getTeaTeachCourse(paramMap);
			if(courseList!=null&&courseList.size()!=0){
				returnMap.put("courseList", courseList);
				paramMap.put("courseList", courseList);
				System.out.println("最后的map"+paramMap);
				
				//查询问题列表
				List<Map<String, Object>> problemList = problemDao.getAllProblemList(paramMap);
//				if(problemList!=null&&problemList.size()!=0){
//					for(int i=0;i<problemList.size();i++){
//						System.out.println("做转义字符处理之前"+problemList.get(i).get("problemContent"));
//						//转义字符处理
//						String str = java.net.URLEncoder.encode(String.valueOf(problemList.get(i).get("problemContent")),"UTF-8");
//						problemList.get(i).put("problemContent", str);
//						System.out.println("做转义字符处理之后"+problemList.get(i).get("problemContent"));
//					}
//				}
				returnMap.put("problemList", problemList);
				returnMap.put("pageNow", pageNow);
				
				//获取全部页数
				paramMap.put("pageNow", 0);
				List<Map<String, Object>> allMemberList = problemDao.getAllProblemList(paramMap);
				int totalPage = pageUtil.getTotalPage(allMemberList, pageSize);
				returnMap.put("totalPage", totalPage);
				returnMap.put("errFlag", "success");
			}else{
				returnMap.put("errFlag", "fail");
				returnMap.put("errMsg", "当前课程暂无问题");
			}
			
		} catch (Exception e) {
			throw e;
		}
		return returnMap;
	}
	
	/**
	 * @param 问题详情
	 * @throws IOException
	 */
	public Map<String, Object> getProblemDetail(Map<String, Object> paramMap,HttpSession session) throws Exception {

		Map<String, Object> returnMap = new HashMap<String, Object>();
		System.out.println("前台给我传的参数"+paramMap);
		try {
			//获取分页数据
			int pageNow = (Integer)paramMap.get("pageNow");
			int pageSize = (Integer)paramMap.get("pageSize");
			int startNum = pageUtil.getSqlStartPos(pageNow, pageSize);
			paramMap.put("startNum", startNum);
			String content="";
			//通过问题主键获取问题详情
			Map<String, Object> problemMap = problemDao.getProblemDetail(paramMap);
//			if(problemMap!=null){
//				//转义字符处理
//				content =java.net.URLDecoder.decode(String.valueOf( problemMap.get("problemContent")),"UTF-8");
//				problemMap.put("problemContent", content);
//			}
			returnMap.put("problemMap", problemMap);
			System.out.println("最后的map"+paramMap);
			returnMap.put("pageNow", pageNow);
			//通过问题主键获取此问题的答案列表
			List<Map<String,Object>> answerList = answerDao.getAllAnswerByProId(paramMap);
//			for(int i=0;i<answerList.size();i++){
//				String str = java.net.URLDecoder.decode(String.valueOf(answerList.get(i).get("answerContent")),"UTF-8");
//				answerList.get(i).put("answerContent", str);
//						
//			}
			returnMap.put("answerList", answerList);
			//获取当前登陆人的学号/工号
			String stuorteaId = (String) session.getAttribute("stuorteaId");
//			String stuorteaId="8888888";
			returnMap.put("stuorteaId", stuorteaId);
			//获取全部页数
			paramMap.put("pageNow", 0);
			List<Map<String, Object>> allanswerList = answerDao.getAllAnswerByProId(paramMap);
			int totalPage = pageUtil.getTotalPage(allanswerList, pageSize);
			returnMap.put("totalPage", totalPage);
		} catch (Exception e) {
			throw e;
		}
		return returnMap;
	}
	
	/**
	 *  回答问题表单提交。
	 * @throws IOException
	 */
	public boolean answerProblem(Map<String,Object> paramMap) throws Exception {
		
		boolean flag = false;
		
		try {
			
//			String str = java.net.URLDecoder.decode(String.valueOf(paramMap.get("answerContent")),"UTF-8");
//			paramMap.put("answerContent", str);
			flag = answerDao.answerProblem(paramMap);
			if(flag){
				//查询回答当前问题的个数
				int count = answerDao.getAnsertCount(paramMap);
				System.out.println("回答问题个数是"+count);
				paramMap.put("problemCount", count);
				//向问题表中插入当前问题回答的个数
				flag = problemDao.saveProblem(paramMap);
			}
			
		} catch (Exception e) {
			throw e;
		}
		return flag;
	}
	
	/**
	 *  提问问题表单提交。
	 * @throws IOException
	 */
	public boolean putProblem(Map<String,Object> paramMap) throws Exception {
		
		boolean flag = false;
		
		try {
			//转义字符处理
//			String str = java.net.URLEncoder.encode(String.valueOf(paramMap.get("problemContent")),"UTF-8");
//			paramMap.put("problemContent", str);
			flag = problemDao.putProblem(paramMap);
			
		} catch (Exception e) {
			throw e;
		}
		return flag;
	}
	
	/**
	 * @param pageNow,pageSize(一般是10)，页数从1开始，第一页pageNow传1
	 * @return 问题列表页面（学生）。进页面，翻页，条件查询都是这个。
	 * @throws IOException
	 */
	public Map<String, Object> getStuAllProblemList(Map<String, Object> paramMap,HttpSession session) throws Exception {

		Map<String, Object> returnMap = new HashMap<String, Object>();
		try {
			
			//获取分页数据
			int pageNow = (Integer)paramMap.get("pageNow");
			int pageSize = (Integer)paramMap.get("pageSize");
			int startNum = pageUtil.getSqlStartPos(pageNow, pageSize);
			paramMap.put("startNum", startNum);
			//获取当前登陆人的学号/工号
			String stuorteaId = (String) session.getAttribute("stuorteaId");
			returnMap.put("stuorteaId", stuorteaId);
			paramMap.put("stuorteaId", stuorteaId);
			
			//通过学生获取此学生所属课程
			List<Map<String,Object>> courseList = myCourseDao.getStuCourse(paramMap);
			if(courseList.size()!=0&&courseList!=null){
				returnMap.put("courseList", courseList);
				paramMap.put("courseList", courseList);
				System.out.println("最后的map"+paramMap);
				//查询问题列表
				List<Map<String, Object>> problemList = problemDao.getAllProblemList(paramMap);
//				if(problemList!=null&&problemList.size()!=0){
//					for(int i=0;i<problemList.size();i++){
//						//转义字符处理
//						String str = java.net.URLDecoder.decode(String.valueOf(problemList.get(i).get("problemContent")),"UTF-8");
//						problemList.get(i).put("problemContent", str);
//					}
//				}
				returnMap.put("problemList", problemList);
				returnMap.put("pageNow", pageNow);
				
				//获取全部页数
				paramMap.put("pageNow", 0);
				List<Map<String, Object>> allMemberList = problemDao.getAllProblemList(paramMap);
				int totalPage = pageUtil.getTotalPage(allMemberList, pageSize);
				returnMap.put("totalPage", totalPage);
			}else{
				returnMap.put("errFlag", "fail");
				returnMap.put("errMsg", "当前课程暂无问题");
				returnMap.put("totalPage", 0);
			}
			
			
		} catch (Exception e) {
			throw e;
		}
		return returnMap;
	}
	public Map<String, Object> getProblemDetailOther(Map<String, Object> paramMap,HttpSession session, HttpServletRequest request) throws Exception {

		Map<String, Object> returnMap = new HashMap<String, Object>();
		try {
			//获取分页数据
			int pageNow = (Integer)paramMap.get("pageNow");
			int pageSize = (Integer)paramMap.get("pageSize");
			int startNum = pageUtil.getSqlStartPos(pageNow, pageSize);
			paramMap.put("startNum", startNum);
			//获取当前登陆人的学号/工号
			SysUser user = (SysUser) session.getAttribute(USER_INFO);
			if (user==null) {
				returnMap.put("result", "fail");
				returnMap.put("data", "登陆信息获取失败");
				return returnMap;
			}
			String stuorteaId = user.getStuorteaId();
			Map<String, Object> problemMap = problemDao.getProblemDetail(paramMap);
			returnMap.put("problemMap", problemMap);
			returnMap.put("pageNow", pageNow);
			//通过问题主键获取此问题的答案列表
			paramMap.put("stuorteaId",stuorteaId);
			List<Map<String,Object>> answerList = answerDao.getAllAnswerByProId(paramMap);
			returnMap.put("answerList", answerList);
			returnMap.put("stuorteaId", stuorteaId);
			//获取全部页数
			paramMap.put("pageNow", 0);
			List<Map<String, Object>> allanswerList = answerDao.getAllAnswerByProId(paramMap);
			int totalPage = pageUtil.getTotalPage(allanswerList, pageSize);
			returnMap.put("totalPage", totalPage);
		} catch (Exception e) {
			throw e;
		}
		return returnMap;
	}
	public boolean answerProblemOther(Map<String,Object> paramMap, HttpServletRequest request,HttpSession session) throws Exception {

		boolean flag = false;

		try {

//			String str = java.net.URLDecoder.decode(String.valueOf(paramMap.get("answerContent")),"UTF-8");
//			paramMap.put("answerContent", str);
			//获取当前登陆人的学号/工号
			SysUser user = (SysUser) session.getAttribute(USER_INFO);
//			returnMap.put("stuorteaId", stuorteaId);
//			paramMap.put("stuorteaId", stuorteaId);
			//查询当前登陆人的基本信息
//			List<Map<String,Object>> memList = memberDao.getMemberByStuorteaId(stuorteaId);
			if (user==null) {
				return flag;
			}
			String stuorteaId = user.getStuorteaId();
			paramMap.put("stuorteaId",stuorteaId);
			flag = answerDao.answerProblemMoble(paramMap);
			if(flag){
				//查询回答当前问题的个数
				int count = answerDao.getAnsertCount(paramMap);
				paramMap.put("problemCount", count);
				//向问题表中插入当前问题回答的个数
				flag = problemDao.saveProblem(paramMap);
			}

		} catch (Exception e) {
			throw e;
		}
		return flag;
	}
	public Map<String, Object> getAllProblemListOther(Map<String, Object> paramMap,HttpSession session, HttpServletRequest request) throws Exception {

		Map<String, Object> returnMap = new HashMap<String, Object>();
		try {

			//获取分页数据
			int pageNow = (Integer)paramMap.get("pageNow");
			int pageSize = (Integer)paramMap.get("pageSize");
			int startNum = pageUtil.getSqlStartPos(pageNow, pageSize);
			paramMap.put("startNum", startNum);

			List<Map<String, Object>> problemList = problemDao.getAllProblemListOther(paramMap);
			returnMap.put("problemList", problemList);
			returnMap.put("pageNow", pageNow);

			//获取全部页数
			paramMap.put("pageNow", 0);
			List<Map<String, Object>> allMemberList = problemDao.getAllProblemListOther(paramMap);
			int totalPage = pageUtil.getTotalPage(allMemberList, pageSize);
			returnMap.put("totalPage", totalPage);
			returnMap.put("errFlag", "success");

		} catch (Exception e) {
			throw e;
		}
		return returnMap;
	}
}
